package com.dimples.strategy;

import com.dimples.enums.BuildTableEnum;
import com.dimples.strategy.service.CacheFileBuildTableServiceImpl;
import com.dimples.strategy.service.MysqlFileBuildTableServiceImpl;

import org.springframework.stereotype.Component;

import java.util.Map;

import cn.hutool.core.map.MapUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.extra.spring.SpringUtil;
import lombok.extern.slf4j.Slf4j;

/**
 * @author zhongyj <1126834403@qq.com><br/>
 * @date 2021/1/6
 */
@Slf4j
@Component
public class BuildTableStrategyFactory {

    private static final Map<BuildTableEnum, BuildTableStrategyService> BUILD_TABLE_STRATEGY_MAP = MapUtil.newHashMap();

    static {
        BUILD_TABLE_STRATEGY_MAP.put(BuildTableEnum.MYSQL_FILE, SpringUtil.getBean(MysqlFileBuildTableServiceImpl.class));
        BUILD_TABLE_STRATEGY_MAP.put(BuildTableEnum.CACHE_FILE, SpringUtil.getBean(CacheFileBuildTableServiceImpl.class));
    }

    private BuildTableStrategyFactory() {
    }

    public static BuildTableStrategyService getStrategy(BuildTableEnum typeCode) {
        BuildTableStrategyService strategyService = BUILD_TABLE_STRATEGY_MAP.get(typeCode);
        if (ObjectUtil.isEmpty(strategyService)) {
            log.info(StrUtil.format("暂未支持【{}】类型的建表操作", typeCode.getType()));
            throw new RuntimeException(StrUtil.format("暂未支持【{}】类型的建表操作", typeCode.getType()));
        }
        return strategyService;
    }

}
